package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/courses")

public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping
    public Result list(CourseDto courseDto){
        log.info("查询课程列表{}", "courseDto");
        PageResult<Course> pageResult = courseService.listCourses(courseDto);
        return Result.success(pageResult);
    }
    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Integer id) {//添加路径参数注解
        log.info("删除课程{}", "id");
        courseService.deleteById(id);
        return Result.success();
    }
    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        log.info("添加课程{}", "course");
        courseService.insertCourse(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectCourseById(Integer id) {
        log.info("查询课程{}", "id");
        Course course = courseService.selectById(id);
        return Result.success(course);
    }
    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        log.info("更新课程{}", "course");
        courseService.updateCourse(course);
        return Result.success();
    }
    //查询所有课程
    @GetMapping("/list")
    public Result selectAllCourse() {
        log.info("查询所有课程{}", "");
        List<Course> courseList = courseService.selectAllCourse();
        return Result.success(courseList);
    }
    @GetMapping("/subject/{subject}")
    public Result selectCourseBySubject(@PathVariable Integer subject) {
        log.info("查询课程{}", "subject");
        List<Course> courseList = courseService.selectCourseBySubject(subject);
        return Result.success(courseList);
    }
}
